//
//  TableViewController.m
//  英语统考
//
//  Created by unisedu on 15/12/7.
//  Copyright © 2015年 CaoFei. All rights reserved.
//

#import "TableViewController.h"
#import "ViewController.h"
@interface TableViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic,strong)NSArray * list; //通用
@property (nonatomic,strong)NSArray * exam; //模拟测试
@property (nonatomic,strong)NSArray * Item; //题型
@property (nonatomic,strong)NSArray * exam201509; //201509期末考试范围
@property (nonatomic,strong)NSArray * exam201607; //201607期末考试范围
@property (nonatomic,strong)NSArray * xuewei; //学位英语

@end

@implementation TableViewController

-(NSArray*)xuewei{
    if (!_xuewei) {
        _xuewei = @[@{@"fileName":@"13年5月学位英语考试考纲讲解PPT",@"type":@"pptx"},
                        @{@"fileName":@"2014年下半年学位英语考试考前辅导讲义",@"type":@"doc"},
                        @{@"fileName":@"2015.10学位英语考前串讲",@"type":@"doc"},
                        @{@"fileName":@"学位英语考前串讲讲义20150505",@"type":@"docx"},
                        @{@"fileName":@"（京教027）2016年下半年学位英语考试报名通知",@"type":@"doc"},];
    }
    return _xuewei;
}

-(NSArray *)exam201607{
    if (!_exam201607) {
        _exam201607 = @[@{@"fileName":@"JAVA语言与面向对象程序设计",@"type":@"pdf"},
                        @{@"fileName":@"数字电路",@"type":@"pdf"},
                        @{@"fileName":@"考生信息查询系统",@"type":@"pdf"},];
    }
    return _exam201607;
}

-(NSArray*)list{
    if (!_list) {
        _list = @[@{@"fileName":@"大学英语B级统考英语作文63篇",@"type":@"doc"},
                 @{@"fileName":@"大学英语B级考试词汇表",@"type":@"doc"},
                 @{@"fileName":@"2015年12月5号号讲课各题型应对要点 (1)",@"type":@"pdf"}];
    }
    return _list;
}

-(NSArray*)exam{
    if (!_exam) {
        _exam = @[@{@"fileName":@"大学英语B1",@"type":@"doc"},
                  @{@"fileName":@"大学英语B2",@"type":@"doc"},
                  @{@"fileName":@"大学英语B3",@"type":@"doc"},
                  @{@"fileName":@"大学英语B4",@"type":@"doc"},
                  @{@"fileName":@"大学英语B5",@"type":@"doc"},
                  @{@"fileName":@"大学英语B6",@"type":@"doc"},
                  @{@"fileName":@"大学英语B7",@"type":@"doc"},
                  @{@"fileName":@"大学英语B8",@"type":@"doc"},
                  @{@"fileName":@"大学英语B9",@"type":@"doc"},
                  @{@"fileName":@"大学英语B10",@"type":@"doc"}];
    }
    return _exam;
}

-(NSArray*)Item{
    if (!_Item) {
        _Item = @[@{@"fileName":@"交际英语B",@"type":@"doc"},
                  @{@"fileName":@"写作B",@"type":@"doc"},
                  @{@"fileName":@"完型填空B",@"type":@"doc"},
                  @{@"fileName":@"英译汉B",@"type":@"doc"},
                  @{@"fileName":@"词汇与语法B",@"type":@"doc"},
                  @{@"fileName":@"阅读理解B",@"type":@"doc"}];
    }
    return _Item;
}

-(NSArray *)exam201509{
    if (!_exam201509) {
        _exam201509 =@[@{@"fileName":@"201509学期—C  程序设计",@"type":@"doc"},
                       @{@"fileName":@"201509学期—大学英语2",@"type":@"doc"},
                       @{@"fileName":@"201509学期—模拟电路",@"type":@"pdf"}];
    }
    return _exam201509;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - 阅读文档
-(void)fileToRead:(NSString *)_fileName with:(NSString *)_type{
    NSString* path = [[NSBundle mainBundle] pathForResource:_fileName ofType:_type];
    NSURL *url=[NSURL fileURLWithPath:path];
    UIDocumentInteractionController * documentCtrl = [UIDocumentInteractionController  interactionControllerWithURL:url];
    [documentCtrl setDelegate:self];
    [documentCtrl presentPreviewAnimated:YES];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return [self.list count];
            break;
        case 1:
            return [self.exam count];
            break;
        case 2:
            return [self.Item count];
            break;
        case 3:
            return [self.exam201509 count];
            break;
        case 4:
            return [self.exam201607 count];
            break;
        case 5:
            return [self.xuewei count];
            break;
        default:
            return 0;
            break;
    }
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return @"通读";
            break;
        case 1:
            return @"模拟试卷";
            break;
        case 2:
            return @"题目分类";
            break;
        case 3:
            return @"201509期末考试范围";
            break;
        case 4:
            return @"201607期末考试范围";
            break;
        case 5:
            return @"学位英语";
            break;
        default:
            return 0;
            break;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
    switch (indexPath.section) {
        case 0:
            [cell1.textLabel setText:[[self.list objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
            break;
        case 1:
            [cell1.textLabel setText:[[self.exam objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
            break;
        case 2:
            [cell1.textLabel setText:[[self.Item objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
            break;
        case 3:
            [cell1.textLabel setText:[[self.exam201509 objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
            break;
        case 4:
            [cell1.textLabel setText:[[self.exam201607 objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
            break;
        case 5:
            [cell1.textLabel setText:[[self.xuewei objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
            break;
        default:
            break;
    }
    
    
    
    return cell1;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    ViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
    switch (indexPath.section) {
        case 0:
            [self fileToRead:[[self.list objectAtIndex:indexPath.row] objectForKey:@"fileName"]
                        with:[[self.list objectAtIndex:indexPath.row] objectForKey:@"type"]];
//            [detail setFileName:[[self.list objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
//            [detail setType:[[self.list objectAtIndex:indexPath.row] objectForKey:@"type"]];
            break;
        case 1:
            [self fileToRead:[[self.exam objectAtIndex:indexPath.row] objectForKey:@"fileName"]
                        with:[[self.exam objectAtIndex:indexPath.row] objectForKey:@"type"]];
//            [detail setFileName:[[self.exam objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
//            [detail setType:[[self.exam objectAtIndex:indexPath.row] objectForKey:@"type"]];
            break;
        case 2:
            [self fileToRead:[[self.Item objectAtIndex:indexPath.row] objectForKey:@"fileName"]
                        with:[[self.Item objectAtIndex:indexPath.row] objectForKey:@"type"]];
//            [detail setFileName:[[self.Item objectAtIndex:indexPath.row] objectForKey:@"fileName"]];
//            [detail setType:[[self.Item objectAtIndex:indexPath.row] objectForKey:@"type"]];
            break;
        case 3:
            [self fileToRead:[[self.exam201509 objectAtIndex:indexPath.row] objectForKey:@"fileName"]
                        with:[[self.exam201509 objectAtIndex:indexPath.row] objectForKey:@"type"]];
            break;
        case 4:
            [self fileToRead:[[self.exam201607 objectAtIndex:indexPath.row] objectForKey:@"fileName"]
                        with:[[self.exam201607 objectAtIndex:indexPath.row] objectForKey:@"type"]];
            break;
        case 5:
            [self fileToRead:[[self.xuewei objectAtIndex:indexPath.row] objectForKey:@"fileName"]
                        with:[[self.xuewei objectAtIndex:indexPath.row] objectForKey:@"type"]];
            break;
        default:
            break;
    }
    
    
    
    
//    [self.navigationController  pushViewController:detail animated:YES];
}


#pragma mark - UIDocumentInteractionControllerDelegate

- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
    return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
    return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
    
    return self.view.frame;
}
//点击预览窗口的“Done”(完成)按钮时调用

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)_controller
{
    _controller= nil;
}

@end
